/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.ListIterator;
import javax.swing.border.Border;
import main.WinkelApplication;
import model.User;

/**
 *
 * @author Jack Sierkstra
 */
public class UserPanel extends JPanel {   
    
    private JTable table;
    private Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    private String username;
    
    public UserPanel() {
      super();
      setLayout(new BorderLayout());
      addTitle();
      table();
      addButtonPane();
    }    
    public void table() {
         
    
        
        String[] columnames = {"Username","Password","AccesLevel"};
  
        java.util.List<User> users = WinkelApplication.getQueryManager().getUsers();
        Object[][] data = new Object[users.size()][];
        
        for (int i = 0; i < users.size(); i++)
        {
            User k = users.get(i);
            String $acceslevel;
            if (k.getAccesLevel()==1){
                $acceslevel =  "Kassiér";
            } else if (k.getAccesLevel()==2){
                $acceslevel =  "VoorraadBeheerder";
            } else {
                $acceslevel =  "Manager";
            }
            data[i] = new Object[] { k.getUsername(),k.getPassword(), $acceslevel};
        }
        
        table = new JTable(data, columnames) {
            @Override
            public boolean isCellEditable(int row, int column){  
                return false;  
            }  
        };
        table.setPreferredScrollableViewportSize(new Dimension((dim.width - 50), (dim.height - 40)));
        
        table.setRowHeight(25);
        table.setFont(WinkelApplication.FONT_14_PLAIN);
        table.setFillsViewportHeight(true);
        
        
        JScrollPane scrollpane = new JScrollPane(table);
       
        add(scrollpane, BorderLayout.CENTER);
        
        table.addMouseListener(new MouseAdapter() {
            @Override
          public void mouseClicked(MouseEvent e) {
           
              JTable target = (JTable)e.getSource();
              int row = table.getSelectedRow();
              //int col = table.getSelectedColumn();
              
              
              Object lastobj = table.getModel().getValueAt(row, 0);
              username = lastobj.toString();
                System.out.println(username);
              // do some action if appropriate column
            
          }
        });
        
        
    }
    
     
    public void userEdit(String username) {
        if(username != null) {
            JFrame frame = new JFrame();
            frame.add(new view.UserEdit(username));
            frame.setResizable(false);
            frame.setLocation((dim.width/2)-230, (dim.height/2)-230);
            frame.setSize(430, 250);
            frame.setVisible(true);
            
        } else {
            JOptionPane.showMessageDialog(null,
            "Er moet een gebruiker geselecteerd zijn om te wijzigen.","Selecteer een gebruiker",
            JOptionPane.INFORMATION_MESSAGE);  
        }
    }
    
    public void userRemove(String username) {
        if(username != null) {
            int response = JOptionPane.showConfirmDialog(
                        null,
                        "Weet je zeker dat je deze gebruiker wilt verwijderen?",
                        "Verwijder gebruiker",
                        JOptionPane.YES_NO_OPTION
         );          
            if (response == JOptionPane.YES_OPTION )
                    {
                    WinkelApplication.getQueryManager().removeUser(username);  
                    WinkelApplication.getInstance().showPanel(new view.UserPanel());
                    JOptionPane.showMessageDialog(null,
                    "De gebruiker is verwijderd!","Verwijderd",
                    JOptionPane.INFORMATION_MESSAGE);
                    } 
        
        WinkelApplication.getInstance().showPanel(new view.UserPanel());
        } else {
            JOptionPane.showMessageDialog(null,
            "Er moet een gebruiker geselecteerd zijn om te verwijderen.","Selecteer een gebruiker",
            JOptionPane.INFORMATION_MESSAGE);  
        }
    }

    private void addButtonPane() {
        
        JPanel frame = new JPanel();
        frame.setPreferredSize(new Dimension(dim.width, 60));
        
        JButton toevoegen = new JButton("Nieuwe gebruiker");
        
        toevoegen.setPreferredSize(new Dimension(150,50));
        toevoegen.addMouseListener(new MouseAdapter() {
          
          @Override
          public void mouseClicked(MouseEvent e) {   
             JFrame frame = new JFrame();
             frame.add(new view.UserAdd());
             frame.setResizable(false);
             frame.setLocation((dim.width/2)-230, (dim.height/2)-230);
             
             frame.setSize(430, 230);
             frame.setVisible(true);              
          }
        });
        
        JButton terug = new JButton("Terug naar beheer");
        
        terug.setPreferredSize(new Dimension(150,50));
        terug.addMouseListener(new MouseAdapter() {
          
          @Override
          public void mouseClicked(MouseEvent e) {   
             WinkelApplication.getInstance().showPanel(new view.Manage(3));
          }
        });
        
        JButton wijzig = new JButton("Wijzig");
        wijzig.setAlignmentX(LEFT_ALIGNMENT);
        wijzig.setPreferredSize(new Dimension(150,50));
        wijzig.addMouseListener(new MouseAdapter() {
          
          @Override
          public void mouseClicked(MouseEvent e) {   
              userEdit(username);

          }
        });
        
        JButton verwijder = new JButton("Verwijder");
        verwijder.setAlignmentX(LEFT_ALIGNMENT);
        verwijder.setPreferredSize(new Dimension(150,50));
        verwijder.addMouseListener(new MouseAdapter() {
          
          @Override
          public void mouseClicked(MouseEvent e) {   
              userRemove(username);
              System.out.println(username);

          }
        });
        
        frame.add(toevoegen);
        frame.add(wijzig);
        frame.add(verwijder);
        frame.add(terug);
        
        add(frame, BorderLayout.PAGE_END);
        
    }

    private void addTitle() {
        JLabel titel = new JLabel("Gebruikers beheren", SwingConstants.CENTER);
        titel.setFont(WinkelApplication.getInstance().computeFontSize(2));
        
        JPanel frame = new JPanel();
        frame.setLayout(new BoxLayout(frame, BoxLayout.Y_AXIS));
        
        
        frame.add(titel);
        
        titel.setAlignmentX(Component.CENTER_ALIGNMENT);
       
        add(frame, BorderLayout.PAGE_START);
    }
    
   
}